package com.nth.rooster.circles.controllers;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nth.rooster.circles.services.CircleServiceI;
import com.nth.rooster.core.entities.RamCircles;
import com.nth.rooster.core.pojo.CircleBeans;
import com.nth.rooster.core.pojo.User;

@Controller
@RequestMapping("/circles")
public class Circles {
	static Logger LOGGER = Logger.getLogger(Circles.class);

	@Autowired
	private CircleServiceI circleServiceI;

	@RequestMapping(value = "/createCircles", method = RequestMethod.POST)
	@ResponseBody
	public Long createCircle(@RequestBody CircleBeans circleBeans,@RequestParam Long userID) {
		LOGGER.debug("Entered::Circle::Circles::createCircle(CircleBeans circleBeans) ");
		Long circleID = circleServiceI.createCircle(circleBeans,userID);
		LOGGER.debug("Exited::Circle::Circles::createCircle(CircleBeans circleBeans) ");
		return circleID;
	}
	
	@RequestMapping(value = "/getCircleByCircleID/{id}", method = RequestMethod.POST)
	@ResponseBody
	public RamCircles getCircleByCircleID(@PathVariable Long id) {
		LOGGER.debug("Entered::Circle::Circles::getCircleByCircleID()");
		RamCircles circle = circleServiceI.getCircleByCircleID(id);
		LOGGER.debug("Exited::Circle::Circles::getCircleByCircleID()");
		return circle;
	}
	
	@RequestMapping(value = "/getAllCircles/{withDescription}", method = RequestMethod.POST)
	@ResponseBody
	public List<CircleBeans> getAllCircles(@PathVariable Boolean withDescription) {
		LOGGER.debug("Entered::Circle::Circles::getCircles()");
		List<CircleBeans> circles = circleServiceI.getAllCircles(withDescription);
		LOGGER.debug("Exited::Circle::Circles::getCircles()");
		return circles;
	}
	
	@RequestMapping(value = "/updateCircle/{userID}", method = RequestMethod.POST)
	@ResponseBody
	public Long updateCircle(@PathVariable Long userID, @RequestBody CircleBeans circleBeans) {
		LOGGER.debug("Entered::Circle::Circles::assignOwnersToCircle()");
		Long circleID=circleServiceI.updateCircle(circleBeans, userID);
		LOGGER.debug("Exited::Circle::Circles::assignOwnersToCircle()");
		return circleID;
	}

	@RequestMapping(value = "/assignOwnersToCircle/{id}", method = RequestMethod.POST)
	@ResponseBody
	public void assignOwnerToCircle(@PathVariable Long id,@RequestBody List<User> users) {
		LOGGER.debug("Entered::Circle::Circles::assignOwnersToCircle()");
		circleServiceI.assignOwnerToCircle(id, users);
		LOGGER.debug("Exited::Circle::Circles::assignOwnersToCircle()");
	}
	
	@RequestMapping(value = "/getCircles", method = RequestMethod.POST)
	@ResponseBody
	public CircleBeans getCircle() {
		LOGGER.debug("Entered::Circle::Circles::createCircle(CircleBeans circleBeans) ");
		
		CircleBeans beans = new CircleBeans();
		beans.setCircleID(1l);
		beans.setCircleDescrition("ALL the students of tenth class will be in first class");
		beans.setCircleTitle("11th class");
		
		User user = new User();
		user.setUserID(1l);
		user.setFirstName("Krishna");
		user.setLastName("Meghana");
		
		User user2 = new User();
		user2.setUserID(1l);
		user2.setFirstName("Krishna");
		user2.setLastName("Meghana");
		
		List<User> list = new ArrayList<User>();
		list.add(user);
		list.add(user2);
		
		beans.setAdmins(list);
		
		LOGGER.debug("Exited::Circle::Circles::createCircle(CircleBeans circleBeans) ");
		
		return beans;
	}

	public CircleServiceI getCircleServiceI() {
		return circleServiceI;
	}

	public void setCircleServiceI(CircleServiceI circleServiceI) {
		this.circleServiceI = circleServiceI;
	}
	
	

}
